package com.fancy.middleware.common.product;

import com.fancy.middleware.common.beans.Domain;
import com.fancy.middleware.common.beans.DomainWrapper;
import com.lmax.disruptor.RingBuffer;
import io.netty.channel.ChannelHandlerContext;

public class FrameDataEventProducer<T extends DomainWrapper> {

    private final RingBuffer<T> ringBuffer;

    public FrameDataEventProducer(RingBuffer<T> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void onData(Domain domain, ChannelHandlerContext ctx) {
        long sequence = ringBuffer.next();
        try {
            DomainWrapper wrapper = ringBuffer.get(sequence);
            wrapper.setDomain(domain);
            wrapper.setChannelHandlerContext(ctx);
        } finally {
            ringBuffer.publish(sequence);
        }
    }
}
